#include "kernel_operator.h"
#include <type_traits>
using namespace AscendC;
constexpr int32_t BUFFER_NUM = 2;
template<typename TYPE_Y> class KernelAsinh {
    using T = TYPE_Y;
public:
    __aicore__ inline KernelAsinh() {}
    __aicore__ inline void Init(GM_ADDR x, GM_ADDR y, 
                            uint32_t CoreDataNum, uint32_t finalTileNum, uint32_t tileDataNum, uint32_t TailDataNum) {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");
        this->coreDataNum = CoreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = TailDataNum;

        xGm.SetGlobalBuffer((__gm__ DTYPE_X*)x, this->coreDataNum);
        yGm.SetGlobalBuffer((__gm__ DTYPE_Y*)y, this->coreDataNum);

        pipe.InitBuffer(inQueueX, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_X));
        pipe.InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_Y));
        // if constexpr (std::is_same_v<T, int8_t> || std::is_same_v<T, signed char>) {
            pipe.InitBuffer(QueueTmp1, this->tileDataNum * sizeof(float));
            pipe.InitBuffer(QueueTmpbit, this->tileDataNum * sizeof(uint8_t));
            pipe.InitBuffer(QueueTmpX, this->tileDataNum * sizeof(float));
            pipe.InitBuffer(QueueTmpY, this->tileDataNum * sizeof(float));
        // }
        // else if constexpr (std::is_same_v<T, int32_t>)
        // {
        //     pipe.InitBuffer(QueueTmp1, this->tileDataNum * sizeof(float));
        //     pipe.InitBuffer(QueueTmp2, this->tileDataNum * sizeof(float));
        // }
    }
    __aicore__ inline void Process() {
        int32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (int32_t i = 0; i < loopCount; i++) {
            if (i == this->tileNum - 1) {
              this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress) {
        LocalTensor<DTYPE_X> xLocal = inQueueX.AllocTensor<DTYPE_X>();
        DataCopy(xLocal, xGm[progress * this->tileDataNum], this->processDataNum);
        inQueueX.EnQue(xLocal);
    }
    __aicore__ inline void Compute(int32_t progress) {
        LocalTensor<DTYPE_X> xLocal = inQueueX.DeQue<DTYPE_X>();
        LocalTensor<DTYPE_Y> yLocal = outQueueY.AllocTensor<DTYPE_Y>();
    if constexpr (std::is_same_v<T, float>) {
        auto tmpbit = QueueTmpbit.Get<uint8_t>();
        auto tmp1 = QueueTmp1.Get<DTYPE_X>();
        Mul(yLocal, xLocal, xLocal, this->processDataNum);
        Adds(yLocal, yLocal, (DTYPE_X)1, this->processDataNum);
        Sqrt(yLocal, yLocal, this->processDataNum);
        Abs(tmp1, xLocal, this->processDataNum);
        Add(yLocal, yLocal, tmp1, this->processDataNum);
        Ln(yLocal, yLocal, this->processDataNum);
        CompareScalar(tmpbit, xLocal, (DTYPE_X)0, AscendC::CMPMODE::LT, this->processDataNum);
        Duplicate(tmp1, (DTYPE_X)(-1), this->processDataNum);
        Select(xLocal, tmpbit, tmp1, (DTYPE_X)(1), SELMODE::VSEL_TENSOR_SCALAR_MODE, this->processDataNum);
        Mul(yLocal, xLocal, yLocal, this->processDataNum);
    }
    else
    {
        auto tmp1 = QueueTmp1.Get<float>();
        auto tmpbit = QueueTmpbit.Get<uint8_t>();
        auto tmpx = QueueTmpX.Get<float>();
        auto tmpy = QueueTmpY.Get<float>();
        Cast(tmpx, xLocal, RoundMode::CAST_NONE, this->processDataNum);
        
        Mul(tmpy, tmpx, tmpx, this->processDataNum);
        Adds(tmpy, tmpy, (float)1, this->processDataNum);
        Sqrt(tmpy, tmpy, this->processDataNum);
        Abs(tmp1, tmpx, this->processDataNum);
        Add(tmpy, tmpy, tmp1, this->processDataNum);
        Ln(tmpy, tmpy, this->processDataNum);
        CompareScalar(tmpbit, tmpx, (float)0, AscendC::CMPMODE::LT, this->processDataNum);
        Duplicate(tmp1, (float)(-1), this->processDataNum);
        Select(tmpx, tmpbit, tmp1, (float)(1), SELMODE::VSEL_TENSOR_SCALAR_MODE, this->processDataNum);
        Mul(tmpy, tmpx, tmpy, this->processDataNum);

        Cast(yLocal, tmpy, RoundMode::CAST_NONE, this->processDataNum);
    }
        

        inQueueX.FreeTensor(xLocal);
        outQueueY.EnQue<TYPE_Y>(yLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress) {
        LocalTensor<DTYPE_Y> yLocal = outQueueY.DeQue<DTYPE_Y>();
        DataCopy(yGm[progress * this->tileDataNum], yLocal, this->processDataNum);
        outQueueY.FreeTensor(yLocal);
    }

private:
    TPipe pipe;
    TQue<QuePosition::VECIN, BUFFER_NUM> inQueueX;
    TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueY;
    TBuf<QuePosition::VECCALC> QueueTmp1, QueueTmpbit, QueueTmpX, QueueTmpY, boox;

    GlobalTensor<DTYPE_X> xGm;
    GlobalTensor<DTYPE_Y> yGm;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};
extern "C" __global__ __aicore__ void asinh(GM_ADDR x, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    // TODO: user kernel impl
    KernelAsinh<DTYPE_Y> op;
    op.Init(x, y, 
            tiling_data.CoreDataNum, tiling_data.finalTileNum, tiling_data.tileDataNum, tiling_data.TailDataNum);
    op.Process();
}